"""
Case Type   : 客户场景-邮储
Case Name   : 更改事务隔离级别，设置default_transaction_isolation='repeatable read'，重启数据库
Create At   : 2021/11/25
Owner       : wan005
Description :
    1、进入postgresql.conf，修改default_transaction_isolation='repeatable read'
    2、重启数据库
    gs_om -t stop && gs_om -t start
    3、等待30s，查看是否产生core
    cat /proc/sys/kernel/core_pattern（查看core路径）
Expect      :
    1、修改成功
    2、数据库重启成功
    3、无core产生
History     :
"""
import time
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Finance(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.user_node = Node("PrimaryDbUser")
        self.root_node = Node("PrimaryRoot")
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        status = self.commonsh.restart_db_cluster()
        self.log.info(status)
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)

        self.param = "default_transaction_isolation"
        result = self.commonsh.execut_db_sql(f"show {self.param}")
        self.isolation = result.splitlines()[-2].strip()

    def test_finance(self):
        text = f"获取core路径"
        self.log.info(text)
        cmd = "cat /proc/sys/kernel/core_pattern"
        self.log.info(cmd)
        result = self.common.get_sh_result(self.user_node, cmd)
        self.log.info(result)
        self.assertNotIn("bash", result, "执行失败:" + text)

        core_path = result.replace(result.split('/').pop(), "")
        self.log.info("core_path:" + core_path)

        cmd = f'ls -l {core_path}|grep "^-"| wc -l'
        num_before = self.common.get_sh_result(self.user_node, cmd)
        self.log.info(num_before)

        text = f"--step1:修改{self.param}='repeatable read' 重启; expect:成功"
        self.log.info(text)
        result = self.commonsh.execute_gsguc("set",
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.param}="
                                             f"'repeatable read'")
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)
        status = self.commonsh.restart_db_cluster()
        self.log.info(status)

        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)

        text = "--step3:等待30s，查看是否产生core; expect:失败"
        self.log.info(text)
        time.sleep(30)
        cmd = f'ls -l {core_path}|grep "^-"| wc -l'
        num_after = self.common.get_sh_result(self.user_node, cmd)
        self.log.info(num_after)
        self.assertEqual(num_before, num_after, "执行失败:" + text)

    def tearDown(self):
        text = "--step3:清理环境; expect:成功"
        self.log.info(text)
        result = self.commonsh.execute_gsguc("set",
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.param}="
                                             f"'{self.isolation}'")
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + text)
        status = self.commonsh.restart_db_cluster()
        self.log.info(status)
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.log.info(f"-----{os.path.basename(__file__)} finish=")
